/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gr.context;

import com.gr.search.Connector;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hieudt
 */
public class Profile {

    private int id;
    private String name;
    private String job;
    private int age;
    private String gender;
    private String[] interest;

    public Profile(int id, String name, String gender, int age, String job) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.job = job;
        this.gender = gender;
        System.out.println(this.interest);
    }

    public Profile(int id) {
        this.id = id;
        loadProfile();
    }

    public String[] getInterest() {
        return interest;
    }

    public String getName() {
        return this.name;
    }

    public int getID() {
        return this.id;
    }

    public void setInterest(String[] i) {
        this.interest = i;
    }

    private void loadProfile() {
        try {
            Connection connect = Connector.connect2();
            Statement s = (Statement) connect.createStatement();
            Profile p = null;
            s.executeQuery("SELECT id, name, age, gender, job FROM profile where id =" + id);
            ResultSet rs = s.getResultSet();
            while (rs.next()) {
                // int idVal = rs.getInt (1); 
                this.name = rs.getString(2);  // String nameVal = rs.getString (2); 
                this.age = rs.getInt(3);
                this.gender = rs.getString(4);
                this.job = rs.getString(5);
                this.setInterest(loadInterest());
                System.out.println("Profile: " + this.toString());
            }
            rs.close();
            s.close();
            connect.close();
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Connect Error [loadProfile]!");
        }
    }

    private String[] loadInterest() {
        try {
            Connection connect = Connector.connect2();
            Statement s = (Statement) connect.createStatement();
            String[] buf = new String[15];
            s.executeQuery("SELECT term, boost FROM interest where user_id =" + id + " order by boost DESC");
            ResultSet rs = s.getResultSet();
            int i = 0;
            while (rs.next()) {                         // int idVal = rs.getInt (1); 
                String termVal = rs.getString("term");  // String nameVal = rs.getString (2); 
                buf[i] = termVal;
                System.out.println("Term: " + termVal + ".\n");
                i++;
                if (i > 4) {
                    break;
                }
            }
            rs.close();
            s.close();
            connect.close();
            System.out.println(buf.toString());
            return buf;
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Connect Error [loadInterest]!");
            return null;
        }
    }

    public static int checkProfile(String name, String pw) {
        try {
            Map<String, String> userList = new HashMap<>();
            Map<String, Integer> idList = new HashMap<>();
            Connection connect = Connector.connect2();
            Statement s = (Statement) connect.createStatement();
            System.out.println(name + "--" + pw);
            s.executeQuery("SELECT id,name, pw FROM profile");
            ResultSet rs = s.getResultSet();
            int check = 0;
            while (rs.next()) {
                // int idVal = rs.getInt (1); 
                int i = rs.getInt(1);
                String n = rs.getString(2);  // String nameVal = rs.getString (2); 
                String p = rs.getString(3);
                userList.put(n, p);
                idList.put(n, i);
                System.out.println("********\n" + n + "--" + p);
            }
            String p_test = userList.get(name);
            if (p_test != null) {
                if (p_test.compareTo(pw) == 0) {
                    check = idList.get(name).intValue();
                }
            }
            rs.close();
            s.close();
            connect.close();
            return check;
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Connect Error [loadProfile]!");
            return 0;
        }
    }

    public static void insertProfile(String name, String password, String gender, int age, String job) {
        try {
            Connection con = Connector.connect2();
            java.sql.PreparedStatement stmt = con.prepareStatement("INSERT INTO profile(name, pw, age, gender, job) VALUES(?, ?,?,?,?);");
            stmt.setString(1, name);
            stmt.setString(2, password);
            stmt.setInt(3, age);
            stmt.setString(4, gender);
            stmt.setString(5, job);
            //stmt.setString(3,URLs);
            stmt.executeUpdate();
            con.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(History.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException e) {
            System.err.println("Cant insert profile!!!");
        }
    }

    public static void insertInterest(int id, String[] interest) {
        try {
            Connection con = Connector.connect2();
            java.sql.PreparedStatement stmt = con.prepareStatement("INSERT INTO interest(user_id, term, boost) VALUES(?, ?,?);");
            for (int i = 0; i < interest.length; i++) {
                stmt.setInt(1, id);
                stmt.setString(2, interest[i]);
                stmt.setInt(3, 10);
                stmt.executeUpdate();
            }
            con.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(History.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException e) {
            System.err.println("Cant insesrt interest!!!");
        }
    }

    @Override
    public String toString() {
        return "UserID: " + id + "\n" + "Username: " + name + "\n" + "gender: " + gender + "\n" + "Age: " + age + "\n" + "job: " + job + "\n" + "Interest: " + interest + "\n";
    }
}
